package com.hkyp.ss.coursewebadmin.utils;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 产品编号  & 申请编号  生成
 */
public class NumberGeneratorUtil {

    private static SecureRandom secureRandom;

    static {
        try {
            secureRandom = SecureRandom.getInstance("SHA1PRNG");
            //这个类生成随机数，首次调用性能比较差，如果条件允许最好服务启动后先调用一下nextInt()
            secureRandom.nextInt();
        } catch (NoSuchAlgorithmException e) {
            System.out.println("RandomUtil did not work!");
        }
    }

    public static String getProductNumber () {

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = format.format(new Date());
        return date;
    }

    public static String getCourseNumber(){

        int bound = (int) Math.pow(10, 4);
        // 产生一个10的N次方的随机数
        String random = String.valueOf(secureRandom.nextInt(bound));
        //如果生成的随机数位数不够，前面加0补足
        for (int i = random.length(); i < 4; i++) {
            random = "0" + random;
        }


        return getProductNumber()+random;


    }

    public static String getRandomNumber(int x){

        int bound = (int) Math.pow(10, x);
        // 产生一个10的N次方的随机数
        String random = String.valueOf(secureRandom.nextInt(bound));
        //如果生成的随机数位数不够，前面加0补足
        for (int i = random.length(); i < x; i++) {
            random = "0" + random;
        }
        return random;
    }

    public static void main(String[] args) {
        System.out.println(getRandomNumber(4));
    }
}
